package net.sk.china.front.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sk.china.common.response.CommonResult;
import net.sk.china.common.utils.TalkUtil;
import net.sk.china.front.request.*;
import net.sk.china.front.response.LoginResponse;
import net.sk.china.front.response.ValidateCodeResponse;
import net.sk.china.front.service.LoginService;
import net.sk.china.front.strategy.AccountService;
import net.sk.china.front.strategy.PcService;
import net.sk.china.front.strategy.SmsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/4 16:59
 * // +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("/")
@Api(tags = "登录注册相关")
public class LoginController {

    private final LoginService loginService;

    private final SmsService smsService;

    private final AccountService accountService;

    private final PcService pcService;

    public LoginController(LoginService loginService, SmsService smsService, AccountService accountService, PcService pcService) {
        this.loginService = loginService;
        this.smsService = smsService;
        this.accountService = accountService;
        this.pcService = pcService;
    }

    @ApiOperation(value = "登录 - 四种登录方式")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public CommonResult<LoginResponse> login(@RequestBody @Validated LoginRequest login, HttpServletRequest request) {
        String ip = TalkUtil.getClientIp(request);

        return CommonResult.success(loginService.login(login, ip));
    }

    @ApiOperation(value = "获取短信验证码")
    @RequestMapping(value = "sendCode", method = RequestMethod.POST)
    public CommonResult<Boolean> sendCode(@RequestBody @Validated SendCodeRequest sendCodeRequest, HttpServletRequest request)
    {
        String ip = TalkUtil.getClientIp(request);
        return CommonResult.success(smsService.sendCode(sendCodeRequest, ip));
    }

    @ApiOperation(value = "移动端找回密码并登录")
    @RequestMapping(value = "retrieve/password", method = RequestMethod.POST)
    public CommonResult<LoginResponse> retrievePassword(@RequestBody @Validated RetrievePwdRequest retrievePwdRequest, HttpServletRequest request) {
        String ip = TalkUtil.getClientIp(request);
        return CommonResult.success(loginService.retrievePassword(retrievePwdRequest, ip));
    }

    @ApiOperation(value = "手机号注册")
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public CommonResult<LoginResponse> register(@RequestBody @Validated RegisterRequest registerRequest, HttpServletRequest request) {
        String ip = TalkUtil.getClientIp(request);
        return CommonResult.success(accountService.register(registerRequest, ip));
    }

    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    public CommonResult<String> loginOut(HttpServletRequest request)
    {
        accountService.loginOut(request);
        return CommonResult.success();
    }

    @ApiOperation(value = "获取图片验证码")
    @RequestMapping(value = "validate/code", method = RequestMethod.GET)
    public CommonResult<ValidateCodeResponse> code() { return  CommonResult.success(pcService.getValidateCode()); }


    @ApiOperation(value = "PC端个人注册")
    @RequestMapping(value = "personRegister", method = RequestMethod.POST)
    public CommonResult<LoginResponse> personRegister(@RequestBody @Validated PersonRegisterRequest person, HttpServletRequest request) {
        String ip = TalkUtil.getClientIp(request);
        return CommonResult.success(pcService.personRegister(person, ip));
    }

    @ApiOperation(value = "PC端找回密码并登录")
    @RequestMapping(value = "pc/retrieve/password", method = RequestMethod.POST)
    public CommonResult<LoginResponse> pcRetrievePassword(@RequestBody @Validated RetrievePwdRequest retrievePwdRequest, HttpServletRequest request) {
        String ip = TalkUtil.getClientIp(request);
        return CommonResult.success(pcService.retrievePassword(retrievePwdRequest, ip));
    }

    @ApiOperation(value = "用户密码解密")
    @RequestMapping(value = "decryptPassword", method = RequestMethod.GET)
    public CommonResult<String> decryptPassword(@RequestParam(value = "pwd") String pwd, @RequestParam(value = "account") String account) { return CommonResult.success(loginService.decryptPassword(pwd, account)); }
}